home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / ma93 / Voices / Voices.txt < prev   
Encoding:
Text File  |  1993-05-19  |  10.5 KB  |  249 lines

  1. (c) Copyright 1993 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice,
  3. and is provided "as is" without warranty of any kind, either expressed
  4. or implied.  The entire risk as to the use of this information is
  5. assumed by the user.
  6.  
  7.  
  8. Creating Virtual Voices with Amiga Audio
  9.  
  10. by Dan Baker
  11.  
  12.  
  13.  
  14. Every Amiga model comes standard with 4-channel, 8-bit stereo audio
  15. hardware.  This hardware provides every application with the capability
  16. of producing 4-part, stereo sound.  Some applications however may want
  17. to exceed the 4-channel limit.  For games and other applications that
  18. use sound effects extensively, it may be desirable to trigger more than
  19. 4 sounds simultaneously.  This article demonstrates two techniques you
  20. can use to implement virtual voices on the Amiga's audio hardware
  21. effectively doubling the number of voices available to 8.
  22.  
  23.  
  24. Audio Hardware Limits
  25.  
  26. Using DMA, the audio hardware can fetch about 2 bytes per scan line for
  27. each channel without processor intervention.  This means that each
  28. audio channel can play back sampled data at up to 28,867 bytes per
  29. second.  It turns out that this 28,867 byte per second limit far
  30. exceeds the requirements of most audio sample files.
  31.  
  32. The typical 8SVX file contains data sampled at a rate of about 10,000
  33. bytes per second.  In that case, the audio hardware only uses about 1/3
  34. of the available audio bandwidth.  The audio hardware is capable of
  35. fetching much more sound data without affecting system performance.
  36. The question then arises, can this extra horsepower be harnessed in
  37. some way?  The answer is yes.
  38.  
  39.  
  40.  
  41. Audio Interleaving
  42.  
  43. An application can use any extra audio bandwidth to interleave the
  44. bytes from two separate sample files.  This technique allows an
  45. application to play both samples simultaneously on a single audio
  46. channel.
  47.  
  48. For instance, if you have two files sampled at 10,000 bytes per second,
  49. you could set the playback speed to 20,000 bytes per second and
  50. alternate playing bytes from each sample.  The effective speed of each
  51. sample is preserved since each sample is output only half the time.
  52.  
  53.  
  54. [Figure 1]
  55.  
  56.  
  57. As shown in the diagram above, an application interleaves the data
  58. bytes from two sound files into a memory buffer.  The application then
  59. plays the back the interleaved sample at twice the rate.  Note that for
  60. this method to work, the speeds of the two samples should be the same,
  61. or at least a close match.  Another restriction is that the sample
  62. speed must be no more than half the maximum speed available on the
  63. Amiga.  Since the playback speed has to be doubled for interleaving to
  64. work and since the speed limit of the Amiga's built in audio hardware
  65. is 28,867 bytes per second, the speed limit of each interleaved sample
  66. is 14,433 bytes per second.
  67.  
  68. The biggest advantage of audio interleaving is that it exploits unused
  69. audio bandwidth available on the Amiga hardware.  Also, interleaving
  70. does not effect the values of the sampled sound data.  The individual
  71. sound samples remain in their original, noninterleaved form. The
  72. disadvantage is interleaving certain wave forms can combine into an
  73. unexpected and undesirable wave form.  Consider the following two wave
  74. forms:
  75.  
  76.  
  77. [Figure 2]
  78.  
  79.  
  80. When the Amiga interleaves these two wave forms, it has to alternate
  81. between wave form A and wave form B.  Because the Amiga is constantly
  82. oscillating between two wave forms, it produces a completely different
  83. wave form:
  84.  
  85.  
  86. [Figure 3]
  87.  
  88.  
  89. In practice, this usually isn't a problem if you are working with
  90. sampled sound.  Typically, sampled sounds are not pure periodic wave
  91. forms (like the wave forms above).
  92.  
  93.  
  94. Audio Averaging
  95.  
  96. It is not always possible to interleave two samples.  For example, the
  97. frequency of a sample may be more than 14,433 bytes per second, so
  98. doubling it would exceed the 28,867 bytes per second limit.  In that
  99. case, there is another trick you can do to the audio data to combine
  100. two samples on one channel.
  101.  
  102. If the values from each sample file are added and the resulting value
  103. is divided by two, a new data stream can be created which contains the
  104. audio information from both files.
  105.  
  106.  
  107. [Figure 4]
  108.  
  109.  
  110. Note that in the example above, data values are represented as signed
  111. bytes in the range -128 to +127 using twos complement format.  The
  112. resulting average values are correct as shown.  Even though the
  113. ioa_Data field of the IOAudio structure used for all audio.device
  114. requests is shown as (UBYTE *) in the include file <devices/audio.h>,
  115. do not be misled.  The data values are signed bytes.
  116.  
  117. Audio averaging introduces some noise into the resulting combined
  118. signal.  Dividing the summed values by two effectively reduces the
  119. dynamic range of the component samples by one bit (from eight to seven
  120. bits).  Also, when the values are divided by two, any fractional amount
  121. is truncated, hence, some information is lost.
  122.  
  123. Despite these drawbacks, the results of audio averaging on the Amiga's
  124. eight bit audio hardware are comparable to the interleaving technique
  125. described above.  In fact, the two techniques are virtually
  126. indistinguishable on the current generation of Amigas (although this
  127. may not always be true--see the ``Audio Experiments'' section later in
  128. this article).
  129.  
  130.  
  131. Virtual Voices
  132.  
  133. When two samples are combined on a single channel using interleaving or
  134. averaging, both samples are clearly audible but, subjectively, it
  135. sounds as if one bit of volume control has been lost on each sample.
  136. Because of this, it is not wise to carry these virtual voice techniques
  137. to an extreme.  Combining samples without limit will result in a badly
  138. degraded composite in which the component signals are no longer clearly
  139. audible. It is however quite feasible to double the number of available
  140. voices to 8 using interleaving or averaging techniques.  The loss of
  141. fidelity with 8 virtual voices is quite tolerable.  The code listed
  142. below shows how this can be implemented.
  143.  
  144.  
  145. Using the Interplay Program
  146.  
  147. The program, named interplay.c, allows the playback of standard IFF
  148. 8SVX files in three different ways:
  149.  
  150. 1.  Normal playback of a single file on one channel
  151. 2.  Interleaved playback of two files on one channel
  152. 3.  Averaged playback of two files on one channel
  153.  
  154. For normal playback of a single 8SVX file, enter the following command
  155. at the Shell prompt:
  156.  
  157.     1>interplay sample.8svx
  158.  
  159. This feature allows you to find out how a sample sounds alone as
  160. compared with its interleaved or averaged counterpart.
  161.  
  162. For playback of two 8SVX files on a single channel using interleaving,
  163. enter the following command at the Shell prompt:
  164.  
  165.     1>interplay voice.8svx music.8svx
  166.  
  167. The program reads the two files, figures out which has the faster
  168. sampling rate, and sets the audio device to twice that value.  If the
  169. calculated rate exceeds the maximum of 28,867 bytes per second, then
  170. interplay sets the speed to the maximum.  The program then interleaves
  171. the data from the two files so that the bytes played by the audio
  172. channel alternate from one file to the other.  If the data from one
  173. file runs out before the other, any remaining data bytes are
  174. interleaved with zero.
  175.  
  176. For playback of two 8SVX files on a single channel using averaging,
  177. enter the following command at the Shell prompt:
  178.  
  179.     1>interplay voice.8svx music.8svx SUM
  180.  
  181. The ``SUM'' keyword enables averaging instead of interleaving.  In this
  182. case, the speed is set to whichever file uses the faster playback rate.
  183. One byte is taken from each file, the two bytes are added and then
  184. divided by two.  The resulting average value is played back.  If the
  185. data from one file runs out before the other, any remaining data bytes
  186. are averaged with zero.
  187.  
  188. Interplay can play samples of any size.  If a sample is too long, you
  189. can terminate playback by pressing Ctrl-C.
  190.  
  191.  
  192. How Interplay Works
  193.  
  194. Interplay uses a double-buffered approach for the playback of samples
  195. of arbitrary length.  While one data buffer is playing, the other data
  196. buffer is being prepared using either the averaging or interleaving
  197. technique descried above.
  198.  
  199. Most of the code in the main loop within main() is concerned with
  200. switching between one of the two playback buffers and their
  201. corresponding I/O request blocks and message ports.  The averaging or
  202. interleaving of bytes actually takes place in the FillAudio()
  203. subroutine, not in main().
  204.  
  205. The reading and parsing of the 8SVX file are handled by the Parse8svx()
  206. subroutine which takes as a parameter an InterPlay structure.  The
  207. InterPlay struture holds all the state information that the program
  208. needs to manage playback of the sampled data.  Thus there will be one
  209. filled-in InterPlay structure for each file to be played back.  If the
  210. user requests a combined playback, the two InterPlay structures are
  211. linked together via the InterPlay.next_iplay field.  Otherwise this
  212. field is set to NULL.
  213.  
  214. Housekeeping for the audio.device channels used is handled by the
  215. SiezeChannel() and ReleaseChannel() subroutines.
  216.  
  217.  
  218. Audio Experiments
  219.  
  220. Using the interplay.c program listed below, we found that there was
  221. very little difference in the audio quality between the two methods of
  222. combining samples.  We also found that for best results, the dynamic
  223. ranges within the samples themselves had to be closely matched or the
  224. result would be one sample drowning out the other.  Of course, it also
  225. helps if the speeds are a close match.  If they aren't then one or the
  226. other of the samples will be too slow or too fast.
  227.  
  228. Although both of these methods work comparitively well on the Amiga's
  229. eight-bit audio hardware, doing the same tricks with 16-bit samples on
  230. 16-bit hardware would yield a different result.  As mentioned earlier,
  231. the drawback of the averaging method is it loses a bit from the dynamic
  232. range of the sound samples.  Compared to eight-bit sound, this loss is
  233. much less significant when working with 16-bit sound.  As the dynamic
  234. range increases, the impact of losing a single bit from the dynamic
  235. range decreases.  On the other hand, the drawback to the interleaving
  236. method is that it has to oscillate between two samples, which produces
  237. a waveform equal to the sample playback rate.  The waveform is
  238. independent of the dynamic range, so it remains constant as the dynamic
  239. range increases.  The result is the avaeraging method will produce
  240. superior results on systems with greater dynamic range.
  241.  
  242. Perhaps the best thing about the audio techniques demonstrated by
  243. interplay.c is that they are not limited to the Amiga architecture.  In
  244. fact, you can use interleaving and averaging with any system that
  245. supports the variable speed playback of digitally sampled audio.  These
  246. methods will work not only in the current generation of Amigas but in
  247. any future system that support digital audio, although as the dynamic
  248. range increases, the additive method will provide superior sound
  249. quality.